package com.parktech.sun.domain.service.aboutus.impl;

import com.parktech.sun.domain.model.aboutus.Cooperation;
import com.parktech.sun.domain.repository.Page;
import com.parktech.sun.domain.repository.aboutus.CooperationRepository;
import com.parktech.sun.domain.service.aboutus.CooperationService;
import com.parktech.sun.domain.service.impl.BaseServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Property;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * User: Think
 * Date: 11-1-15
 * Time: 下午1:14
 */
@Service
public class CooperationServiceImpl extends BaseServiceImpl<Cooperation> implements CooperationService {

    private CooperationRepository cooperationRepository;

    @Autowired
    public CooperationServiceImpl(CooperationRepository cooperationRepository) {
        super(cooperationRepository);
        this.cooperationRepository = cooperationRepository;
    }

    public Page search(String title, Integer publishStatus, Page page) {

        Criteria criteria = cooperationRepository.createCriteria();

        if (StringUtils.isNotEmpty(title)) {
            criteria.add(Property.forName("name").like(title, MatchMode.ANYWHERE));
        }

        if (publishStatus != -1) {
            criteria.add(Property.forName("publishStatus").eq(publishStatus));
        }

        return cooperationRepository.page(criteria, page);
    }

    public List<Cooperation> listAll() {
        Criteria criteria = cooperationRepository.createCriteria();
        criteria.add(Property.forName("publishStatus").eq(1));
        criteria.addOrder(Order.desc("orderCode"));
        criteria.addOrder(Order.desc("publishDate"));
        return criteria.list();
    }
}
